package web.controller.sys;

import com.github.pagehelper.PageInfo;
import framework.vo.L;
import framework.vo.R;
import framework.vo.ResultInfo;
import framework.vo.ResultList;
import io.swagger.annotations.*;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import system.entity.SysUser;
import system.service.sys.SysUserService;

@RestController
@RequestMapping("/sys/user")
@Api(tags = "账户")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @ApiOperation("添加账户")
    @PostMapping("/add")
    public ResultInfo<Long> add(@ModelAttribute SysUser param) {
        java.util.Date now = new java.util.Date();
        param.setCreateTime(now);
        param.setUpdateTime(now);
        sysUserService.add(param);
        return R.ok(param.getId());
    }

    @ApiOperation("删除账户")
    @PostMapping("/del")
    public ResultInfo<Integer> del(@RequestParam("ids") List<Long> ids) {
        Integer deleted = sysUserService.deleteByIds(ids);
        return R.ok(deleted);
    }

    @ApiOperation("更新账户")
    @PostMapping("/edit")
    public ResultInfo<Integer> edit(@ModelAttribute SysUser param) {
        java.util.Date now = new java.util.Date();
        param.setUpdateTime(now);
        Integer updated = sysUserService.update(param);
        return R.ok(updated);
    }

    @ApiOperation("查看账户")
    @GetMapping("/info")
    public ResultInfo<SysUser> info(@RequestParam("id") Long id) {
        SysUser info = sysUserService.loadById(id);
        return R.ok(info);
    }

    @ApiOperation("账户列表")
    @GetMapping("/list")
    public ResultList<SysUser> list(SysUser param, @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageInfo<SysUser> info = sysUserService.list(param, pageIndex, pageSize);
        return L.ok(info);
    }
}